<script setup>
import { ref, reactive, getCurrentInstance, onMounted, nextTick, watch, render } from "vue"
import { CirclePlus } from '@element-plus/icons-vue';
import {mw_throttle} from '@/utils'
import {ElMessage} from 'element-plus'

const instance = getCurrentInstance()
const _this = instance.appContext.config.globalProperties //vue3获取当前this
const storage = _this.$utils.storage;

import { globalStore } from "@/store/global";
const globalVue = globalStore(); 

// 组件中使用，接收传参的
let propsData = defineProps({
  searchForm: {
    type: Object,
    default: []
  },
  selectModel: {
    type: Array,
    default: []
  },
  selectClassification: {
    type: Array,
    default: []
  },
})
// 组件中使用，接收事件的
const emits = defineEmits(['onSearch', 'addVehicle']);

onMounted(async () => {
  organizationSelect.value = await globalVue.getSelect('organizationSelect', { type: propsData.searchForm.type || '' });
  citySelect.value = await globalVue.getSelect('newCitySelect', { hospitalId: propsData.searchForm.hospitalId || '' });
  selectCamera.value = await globalVue.getSelect('cameraSearchSelect', {type: propsData.searchForm.type || '', hospitalId: propsData.searchForm.hospitalId || '', city: propsData.searchForm.city ? propsData.searchForm.city+'' : ''});
})

const onSubmit = () => {
  function _submit(){
    emits('onSearch')
  }
  mw_throttle(_submit, 1000)
}
const onAdd = () => {
  function _submit(){
    emits('addVehicle')
  }
  mw_throttle(_submit, 1000)
}

const organizationSelect = ref([])
const citySelect = ref([])
const selectCamera = ref([])
async function clearSubmit(num){
  if (num == 1) {
    propsData.searchForm.hospitalId = null;
    propsData.searchForm.city = null;
    propsData.searchForm.camera = null;
    // citySelect.value = []
    organizationSelect.value = await globalVue.getSelect('organizationSelect', { type: propsData.searchForm.type || '' });
    citySelect.value = await globalVue.getSelect('newCitySelect', {});
    selectCamera.value = await globalVue.getSelect('cameraSearchSelect', {type: propsData.searchForm.type || '', hospitalId: propsData.searchForm.hospitalId || '', city: propsData.searchForm.city ? propsData.searchForm.city+'' : ''});
  }else if(num ==2) {
    propsData.searchForm.city = null;
    propsData.searchForm.camera = null;
    citySelect.value = await globalVue.getSelect('newCitySelect', { hospitalId: propsData.searchForm.hospitalId || '' });
    selectCamera.value = await globalVue.getSelect('cameraSearchSelect', {type: propsData.searchForm.type || '', hospitalId: propsData.searchForm.hospitalId || '', city: propsData.searchForm.city ? propsData.searchForm.city+'' : ''});
  }else if(num == 3){
    propsData.searchForm.camera = null;
    selectCamera.value = await globalVue.getSelect('cameraSearchSelect', {type: propsData.searchForm.type || '', hospitalId: propsData.searchForm.hospitalId || '', city: propsData.searchForm.city ? propsData.searchForm.city+'' : ''});
  }
  onSubmit()
}

</script>

<template>
  <el-form :inline="true" :model="searchForm" class="form_search_table">
    <el-form-item label="Classification">
      <el-select class="value_input search_input_130" v-model="searchForm.type" @change="clearSubmit(1)" placeholder="Please select" filterable clearable>
        <el-option v-for="item in selectClassification" :key="item.id" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="Organization">
      <el-select class="value_input" v-model="searchForm.hospitalId" @change="clearSubmit(2)" placeholder="Please select" filterable clearable>
        <el-option v-for="item in organizationSelect" :key="item.id" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="City">
      <el-select v-model="searchForm.city" @change="clearSubmit(3)" placeholder="Please select"  multiple collapse-tags filterable clearable>
        <el-option v-for="item in citySelect" :key="item.id" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="AIBOX Camera">
      <el-select v-model="searchForm.camera" placeholder="Please select" @change="onSubmit" filterable clearable>
        <el-option v-for="item in selectCamera" :key="item.id" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="Search">
      <el-input v-model="searchForm.search" placeholder="Search for vehicle name/code nu..." @keyup.enter="onSubmit" clearable />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">Query</el-button>
      <div class="event_btn">
        <el-button type="primary" v-if="globalVue.isTourist" :icon="CirclePlus" @click="onAdd">Add vehicle</el-button>
      </div>
    </el-form-item>
  </el-form>
</template>